package dao.mysql;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import model.CalculateDTO;
import exception.DAOSystemException;
import dao.CalculateDAO;

//-- Hnin phyho--
public class CalculateDAOImpl implements CalculateDAO{
	
	MySqlImpl mysqlImpl = new MySqlImpl();
	
	
	public CalculateDTO getData(String userId,String type) throws DAOSystemException
	{
		String selectSql=null;
		System.out.println("a.  "+type.trim());
		if((type.trim()).equalsIgnoreCase("Annual Leave"))
		{
			System.out.println("b.  "+type);
		 selectSql = "Select u.Id,l.From ,l.To ,t.type,p.totalAnnualAllowance,u.consumeAnnualAllowance"+
							" From user as u"+ 
							" join `leaveapplicationdb`.`position` as p"+
							" on p.id=u.positionId"+
							" join `leaveapplicationdb`.`leave` as l"+ 
							" on u.id=l.userId"+ 
							" join `leaveapplicationdb`.`leavetype` as t"+ 
							" on t.id=l.leaveTypeId"+ 
							" where t.type='"+type+"'"+
							" and u.id= '"+userId+"'" ;
		}
		else if((type.trim()).equalsIgnoreCase("Medical Leave"))
		{
		    selectSql = "Select u.Id,l.from ,l.to ,t.type,p.totalAnnualAllowance,u.consumeAnnualAllowance"+
								" From user as u"+ 
								" join `leaveapplicationdb`.`position` as p"+
								" on p.id=u.positionId"+
								" join `leaveapplicationdb`.`leave` as l"+ 
								" on u.id=l.userId"+ 
								" join `leaveapplicationdb`.`leavetype` as t"+ 
								" on t.id=l.leaveTypeId"+ 
								" where t.type='"+type+"'"+
								" and u.id= '"+userId+"'" ;
		}
		
		CalculateDTO oneUser = null;
		try{
			ResultSet rs = mysqlImpl.get(selectSql); 
			
			if (rs.next()){
			
				oneUser = new CalculateDTO();
				oneUser.setId(rs.getString("id"));
				oneUser.setFrom(rs.getDate("from"));
				oneUser.setTo(rs.getDate("to"));
				oneUser.setType(rs.getString("type"));
				oneUser.setAllowance(rs.getDouble("totalAnnualAllowance"));
				oneUser.setConsumeAllowance(rs.getDouble("consumeAnnualAllowance"));	
				
			}
			
		} catch (Exception e) {
			String error = "Error selecting users. Nested Exception is: " + e;
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
			throw new DAOSystemException(error);
		} finally {
			try {
				mysqlImpl.close();
			} catch (Exception e) {
			}
		}
		
		return oneUser;
				
	
	}

}
